<template>
	<view class="back-view" @touchmove.stop.prevent="moveHandle">
		<view class="view-background"></view>
		<view class="white-view-back">
			<view class="top-back">
				<view class="top-name">{{pageTitle}}</view>
				<view class="close-back" @click="closeBtnAction">
					<image class="close-img" src="../../../static/close.png"></image>
				</view>
			</view>

			<view class="top-line"></view>
			<scroll-view class="center-back" scroll-y="true">
				<uni-forms ref="baseForm" :modelValue="baseFormData">
					<uni-forms-item label="名称：" labelWidth="100" labelAlign="right" required>
						<view class="item-back ">
							<uni-easyinput v-model="baseFormData.name" placeholder="请填写" />
						</view>
					</uni-forms-item>
					<uni-forms-item label="页面内容：" labelWidth="100" labelAlign="right" required>
						<view class="item-back2 ">
							<myEditor :editorDetail='baseFormData.intro' @getContents="getContents"></myEditor>
							
						</view>
					</uni-forms-item>
				</uni-forms>
				<!-- </view> -->

			</scroll-view>
			<view class="bottom-back">
			<view class="bottom-btn bottom-btn-cancel" @click="closeBtnAction">取消</view>
			<view class="bottom-btn" @click="submitAction">保存</view>
			</view>
		</view>
	</view>
</template>

<script>
	import myEditor from "@/packageB/PC/editor/editor.vue"
	export default {
		components: {
			myEditor
		},
		props: {
			itemID: {
				type: String,
				default: ''
			},
			editType: {
				type: String,
				default: 'add',
			},
		},
		data() {
			return {
				showMask: false,
				pageTitle: "新增微页面",
				baseFormData: {
					'id': '',
					'name': '',
					'intro':''
				},

			}
		},
		mounted() {
			if (this.editType == 'edit') {
				this.pageTitle = '编辑微页面';
				this.baseFormData.id = this.itemID
				this.requestInfoData();
			}
			
		},
		methods: {
			submitAction() {	
				console.log(this.baseFormData)
				this.requestData()
			},
			//网络请求示例
			async requestData() {
				if(!this.baseFormData.name){
					uni.showToast({
						title:'请填写姓名',
						icon:'none'
					})
					return;
				}
				
				if(!this.baseFormData.intro || this.baseFormData.intro == '<p><br></p>'){
					uni.showToast({
						title:'请填写页面内容',
						icon:'none'
					})
					return;
				}
				
				let _this = this
				var parms = {
					'id': this.baseFormData.id,
					'name': this.baseFormData.name,
					'intro':this.baseFormData.intro
				}
				const res = await this.$myRequest({
					url: "/admin/news",
					method: _this.editType == 'edit' ? 'PUT' : 'POST',
					data: parms
				})
				console.log('返回结果', res)
			
				if (res.data.code == 200) {
					uni.showToast({
						title: _this.editType == 'edit' ? '编辑成功' : '添加成功'
					})
					_this.$emit("successBack");
					_this.closeBtnAction()
				}
			},
			//信息网络请求
			async requestInfoData() {
				let _this = this
				const res = await this.$myRequest({
					url: '/admin/news/' + this.itemID,
					method: 'GET',
				})
				console.log('返回结果', res)
				if (res.data.code == 200) {
					_this.baseFormData = res.data.data;
					
				}
			},
			

			
			//编辑器赋值
			getContents(html) {
				this.baseFormData.intro = html;
				console.log(this.baseFormData.intro)
			},
			
			closeBtnAction() {
				this.$emit('addPageClose')
			},
			moveHandle() {
				return true
			},
		},
	}
</script>

<style>
	.back-view {
		position: fixed;
		top: 0;
		right: 0;
		bottom: 0;
		left: 0;
		z-index: 9;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.view-background {
		position: absolute;
		width: 100%;
		height: 100%;
		z-index: 8;
		background-color: rgba(0, 0, 0, 0.3);
	}

	.white-view-back {
		width: 750px;
		height: 600px;
		padding: 24px;
		max-height: 750px;
		background-color: white;
		margin-top: -50px;
		border-radius: 4px;
		z-index: 10;
		position: absolute;
	}

	.top-back {
		display: flex;
		justify-content: space-between;
	}

	.top-name {
		font-size: 18px;
		font-weight: normal;
		color: #333333;

	}

	.close-back {
		cursor: pointer;
		width: 20px;
		height: 20px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.close-img {
		width: 12px;
		height: 12px;
	}

	.top-line {
		height: 1px;
		background: #EEEEEE;
		width: 100%;
		margin-top: 20px;
		margin-bottom: 20px;
	}

	.bottom-back {
		bottom: 0px;
		width: 100%;
		display: flex;
		justify-content: flex-end;
		align-items: center;
		position: absolute;
		height: 60px;
		right: 16px;
	}

	.bottom-btn {
		cursor: pointer;
		width: 104px;
		height: 32px;
		background: #2DA9C5;
		border-radius: 2px;
		color: #FFFFFF;
		font-size: 14px;
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.bottom-btn-cancel {
		cursor: pointer;
		margin-right: 10px;
		background: #FFFFFF;
		border: 1px solid #EEEEEE;
		color: #333333;
	}

	.item-back {
		width: 450px;

	}

	.item-back2 {
		width: 600px;
		border: 1px solid #eee;
		border-radius: 5px;
	}

	.center-back {
		width: 100%;
		height: 500px;
	}
</style>
